﻿@* Generator: Template
    GeneratePrettyNames : true *@
@functions{
    public NFinal.Compile.SqlTemplate.Model.Struct Model { get; set; }
}
public struct __@{@Model.functionName}_@{@Model.varName}__:NFinal.IModel
		{
			@foreach(var field in Model.fields){
			if(field.description!=null){
			@:/// <summary>
            @:/// @{@field.description}
            @:/// </summary>
			}
			if(field.allowNull && field.isValueType){
			@:public @{@field.csharpType}? @{@field.structFieldName};
			}else{
			@:public @{@field.csharpType} @{@field.structFieldName};
			}
			}
			@foreach(var structField in Model.structFields){
			@:public @{@structField.typeName} @{@structField.varName};
			}
			#region Nullable
			private bool __is_null__;
			public bool __Is_Null__
			{
				get {
					return __is_null__;
				}
				set
				{
					 __is_null__=value;
				}
			}
			public static implicit operator __@{@Model.functionName}_@{@Model.varName}__(string __value__)
			{
				var __value2__ = new __@{@Model.functionName}_@{@Model.varName}__();
				__value2__.__Is_Null__ = __value__==null;
				return __value2__;
			}
			public static  bool operator true(__@{@Model.functionName}_@{@Model.varName}__ __value__)
			{
				return !__value__.__is_null__;
			}
			public static bool operator false(__@{@Model.functionName}_@{@Model.varName}__ __value__)
			{
				return !__value__.__is_null__;
			}
			#endregion
			#region 写Json字符串
			public void WriteJson(System.IO.TextWriter tw)
			{
				tw.Write("{");
				@{int i=0;}
				@foreach(var field in Model.fields){
					if(i!=0){
						@:tw.Write(",");
					}
					if(field.jsonType=="Number"){
						@:tw.Write("\"@{@field.structFieldName}\":");
						if(field.allowNull){
						@:tw.Write(@{@field.structFieldName}==null?"null" : @{@field.structFieldName}.ToString());
						}else{
						@:tw.Write(@{@field.structFieldName}.ToString());
						}
					}else if(field.jsonType=="Bool"){
						@:tw.Write("\"@{@field.structFieldName}\":");
						if(field.allowNull){
						@:tw.Write(@{@field.structFieldName}==null?"null":((bool)@{@field.structFieldName}?"true":"false"));
						}else{
						@:tw.Write(@{@field.structFieldName}?"true":"false");
						}
					}else if(field.jsonType=="Time"){
						@:tw.Write("\"@{@field.structFieldName}\":");
						if(field.allowNull){
						@:if(@{@field.structFieldName}==null)
						@:{
							@:tw.Write("null");
						@:}
						@:else
						@:{
							@:tw.Write("\"");
							@:tw.Write(@{@field.structFieldName}.ToString());
							@:tw.Write("\"");
						@:}
						}else{
							@:tw.Write("\"");
							@:tw.Write(@{@field.structFieldName}.ToString());
							@:tw.Write("\"");
						}
					}else if(field.jsonType=="String"){
						@:tw.Write("\"@{@field.structFieldName}\":");
						if(field.allowNull){
						@:if(@{@field.structFieldName}==null)
						@:{
							@:tw.Write("null");
						@:}
						@:else
						@:{
							@:tw.Write("\"");
							@:tw.Write(@{@field.structFieldName});
							@:tw.Write("\"");
						@:}
						}else{
							@:tw.Write("\"");
							@:tw.Write(@{@field.structFieldName});
							@:tw.Write("\"");
						}
					}else if(field.jsonType=="Object"){
						@:tw.Write("\"@{@field.structFieldName}\":");
						if(field.allowNull){
						@:if(@{@field.structFieldName}==null)
						@:{
							@:tw.Write("null");
						@:}
						@:else
						@:{
							@:tw.Write("\"");
							@:tw.Write(@{@field.structFieldName}.ToString());
							@:tw.Write("\"");
						@:}
						}else{
							@:tw.Write("\"");
							@:tw.Write(@{@field.structFieldName}.ToString());
							@:tw.Write("\"");
						}
					}else if(field.jsonType=="Base64"){
						@:tw.Write("\"@{@field.structFieldName}\":");
						if(field.allowNull){
						@:if(@{@field.structFieldName}==null)
						@:{
							@:tw.Write("null");
						@:}
						@:else
						@:{
							@:tw.Write("\"");
							@:tw.Write(Convert.ToBase64String(@{@field.structFieldName}));
							@:tw.Write("\"");
						@:}
						}else{
							@:tw.Write("\"");
							@:tw.Write(Convert.ToBase64String(@{@field.structFieldName}));
							@:tw.Write("\"");
						}
					}
				i++;
				}
				@foreach(var structField in Model.structFields){
					if(structField.isSimple){
						if(structField.isNumber){
							@:tw.Write(",");
							@:tw.Write("\"@{@structField.varName}\":");
							@:tw.Write(@{@structField.varName});
						}else if(structField.isBoolean){
							@:tw.Write(",");
							@:tw.Write("\"@{@structField.varName}\":");
							@:tw.Write(@{@structField.varName}?"true":"false");
						}else{
							@:tw.Write(",");
							@:tw.Write("\"@{@structField.varName}\":");
							@:if(@{@structField.varName}==null)
							@:{
								@:tw.Write("null");
							@:}
							@:else
							@:{
								@:tw.Write("\"");
								@:WriteString(@{@structField.varName}.ToString(),tw);
								@:tw.Write("\"");
						    @:}
						}
					}else{
						if(structField.isList){
							@:tw.Write(",");
							@:tw.Write("\"@{@structField.varName}\":");
							@:@{@structField.varName}.WriteJson(tw);
						}else{
							@:tw.Write(",");
							@:tw.Write("\"@{@structField.varName}\":");
							@:@{@structField.varName}.WriteJson(tw);
						}
					}
				}
				tw.Write("}");
			}
			#endregion
		}
		